package com.utils;

import java.util.Collection;
import java.util.Hashtable;
import java.util.Vector;

public class SoiCau {

	private Collection<String> danhSachCau;
	private Hashtable<String, String> toaDoCau;
	
	
	public Collection<String> getDanhSachCau() {
		return danhSachCau;
	}

	public void setDanhSachCau(Collection<String> danhSachCau) {
		this.danhSachCau = danhSachCau;
	}

	public Hashtable<String, String> getToaDoCau() {
		return toaDoCau;
	}

	public void setToaDoCau(Hashtable<String, String> toaDoCau) {
		this.toaDoCau = toaDoCau;
	}

	// ----
	private String ghepCapDoi(String s1, String s2) {
		if (s1 == null || s2 == null) return "";
		return s1 + s2;
	}
	
	private int indexOf(String strSearch, String strIn, String common) {
		int result = -1;
		if (strSearch == null || strIn == null) return -1;

		// Tim xuoi
		result = strSearch.indexOf(strIn+" ");
		if (result > 0) return result;
		result = strSearch.indexOf(strIn+common);
		if (result > 0) return result;
		result = (strSearch.endsWith(strIn)?strSearch.lastIndexOf(strIn):-1);
		if (result > 0) return result;
		
		// Tim nguoc
		StringBuffer sb = new StringBuffer(strIn);
		strIn = sb.reverse().toString();
		result = strSearch.indexOf(strIn+" ");
		if (result > 0) return result;
		result = strSearch.indexOf(strIn+common);
		if (result > 0) return result;
		result = (strSearch.endsWith(strIn)?strSearch.lastIndexOf(strIn):-1);
		if (result > 0) return result;
		
		return result;
	}
	
	public void soiCau(String[] kqList) {
		if (kqList == null || kqList.length == 0) {
			System.out.println("kqList is empty or null");
			return;
		}
		toaDoCau = new Hashtable<String, String>();
		Vector<String> vCau = new Vector<String>();
		String day0 = kqList[0];
		String[] arrCapDoiStart = {"0","0"};
		String capDoi = null;
		int cauNNgay = kqList.length;
		int ngayThuN = 0;
		int viTriCuaSoThu1 = -1;
		int viTriCuaSoThu2 = -1;
		int viTriCuaCapDoiTrongGiaiCuaNgayTiepTheo = -1;
		
		day0 = kqList[0];
		for (int so1 = 0; so1 <= 9; so1++) {
			arrCapDoiStart[0] = String.valueOf(so1);
			for (int so2 = 0; so2 <= 9; so2++) {
				arrCapDoiStart[1] = String.valueOf(so2);
				capDoi = null;
				viTriCuaSoThu1 = -1;
				viTriCuaSoThu2 = -1;
				viTriCuaCapDoiTrongGiaiCuaNgayTiepTheo = -1;
				// Vi tri xuat phat: vi tri cua so thu 1 va cua so thu 2 trong giai cua ngay dau tien.
				while((viTriCuaSoThu1 = day0.indexOf(arrCapDoiStart[0], viTriCuaSoThu1 + 1))>=0) {
					viTriCuaSoThu2 = viTriCuaSoThu1;
					while((viTriCuaSoThu2 = day0.indexOf(arrCapDoiStart[1], viTriCuaSoThu2 + 1))>=0) {
						// Cap doi la 2 so dang xet ghep lai
						capDoi = ghepCapDoi(arrCapDoiStart[0], arrCapDoiStart[1]);
						// Tim vi tri cua cap doi trong giai cua ngay tiep theo
						for (ngayThuN = 1; ngayThuN < cauNNgay; ngayThuN++) {
							viTriCuaCapDoiTrongGiaiCuaNgayTiepTheo = indexOf(kqList[ngayThuN], capDoi/*cua ngay ngayThuN-1*/, "-");
							// Neu tim thay, thay cap doi dang xet la cap doi tai vi tri so thu 1 va vi tri so thu 2 trong giai tiep theo
							if (viTriCuaCapDoiTrongGiaiCuaNgayTiepTheo > 0) {
								// capDoi=so o viTriCuaSoThu1 va so o viTriCuaSoThu2 trong giai cua ngay ngayThuN
								capDoi = ghepCapDoi(String.valueOf(kqList[ngayThuN].charAt(viTriCuaSoThu1)), 
										String.valueOf(kqList[ngayThuN].charAt(viTriCuaSoThu2)));
								// Tim vi tri cua cap doi trong giai cua ngay cuoi cung
								// Neu tim thay thi la da tim thay CAU
								if (ngayThuN == (cauNNgay-1)) {
									if (!vCau.contains(capDoi)){
										vCau.addElement(capDoi);
										toaDoCau.put(capDoi, viTriCuaSoThu1 + "-" + viTriCuaSoThu2);
									} else {
										toaDoCau.put(capDoi, ((String)toaDoCau.get(capDoi)) + ","+viTriCuaSoThu1 + "-" + viTriCuaSoThu2);
									}
									break;
								}
							} else {
								break;
							}
						}
					}
				}
			}	
		}
		
		// In ket qua CAU tim duoc
		System.out.println("\n=== CAU "+(cauNNgay)+" ngay ===");
		setDanhSachCau(vCau);
		for (int i = 0; i <=9; i++) {
			System.out.print(i+": ");
			for (int j = 0; j <=9; j++) {
				String s = i+""+j;
				if (toaDoCau.get(s) != null) {
					System.out.print(s+"|"+toaDoCau.get(s)+"~~~");
				}
			}
//			for(Iterator<String> it = vCau.iterator(); it.hasNext();){
//				String s = it.next();
//				if (s.startsWith(String.valueOf(i))) {
//					System.out.print(s+"|"+toaDoCau.get(s)+"~~~");
//				}
//			}
			System.out.println();
		}
	}
	
	public static void main(String[] args) {
		String[] kqList = {
				/*19/08/2009*/
				"83467 " +
				"88006 " +
				"19306-63965 " +
				"35974-77700-73243-77887-33819-63571 " +
				"0034-6222-0721-1283 " +
				"8511-4129-6448-2117-6029-9615 " +
				"533-148-190 " +
				"43-50-10-05",
				/*20/08/2009*/
				"69577 " +
				"57571 " +
				"30906-55440 " +
				"53283-56757-84491-61989-95406-58869 " +
				"1194-4546-9967-0152 " +
				"6804-4108-1435-8705-7160-6670 " +
				"858-950-719 " +
				"98-44-71-53",
				/*21/08/2009*/
				"02566 " +
				"51806 " +
				"74864-28169 " +
				"33511-87117-83347-78738-25838-43981 " +
				"7541-1059-6577-7045 " +
				"4473-9494-5053-1785-2442-7362 " +
				"231-341-338 " +
				"51-84-37-13",
				/*22/08/2009*/
				"58947 " +
				"19016 " +
				"23506-46033 " +
				"41590-58927-58358-30123-24154-92974 " +
				"7590-5591-2124-5376 " +
				"8794-0207-4191-4034-4390-7693 " +
				"844-695-925 " +
				"36-50-05-75",
				/*23/08/2009*/
				"33443 " +
				"03531 " +
				"75811-19055 " + // 20 o day
				"61742-86754-21959-83488-04873-85375 " +
				"6142-7737-3709-7925 " +
				"2622-1892-2065-8838-7782-6093 " +
				"872-256-439 " +
				"92-98-26-05",
				/*24/08/2009*/
				"83188 " +
				"92581 " +
				"72025-42652 " +
				"52607-69515-48993-54963-96183-16114 " +
				"6369-7409-8277-9562 " +
				"3538-7709-0152-0288-3211-9491 " +
				"239-725-956 " +
				"94-64-07-31"
				/**/
		};
		
		SoiCau soiCau = new SoiCau();
		soiCau.soiCau(kqList);
	}
}
